<?php
class BannerModel extends Model {

	protected $tableName = 'banner';
	protected $fields = array(0=>'id',1=>'title',2=>'ctime',3=>'url',4=>'image',5=>'status');

	public function getBannerList($limit = 1, $map = array(), $order = "id DESC"){
		// 添加用户表的查询，用于关联查询
		
		if (isset ( $_POST )) {
			$_POST ['id'] && $map ['id'] = intval ( $_POST ['id'] );
			$_POST ['title'] && $map ['title'] = t ( $_POST ['title'] );
			$_POST ['status'] && $map ['status'] = t ( $_POST ['status'] );
		
				
			// 注册时间判断，ctime为数组格式
			if (! empty ( $_POST ['ctime'] )) {
				if (! empty ( $_POST ['ctime'] [0] ) && ! empty ( $_POST ['ctime'] [1] )) {
					// 时间区间条件
					$map ['ctime'] = array (
							'BETWEEN',
							array (
									strtotime ( $_POST ['ctime'] [0] ),
									strtotime ( $_POST ['ctime'] [1] )
							)
					);
				} else if (! empty ( $_POST ['ctime'] [0] )) {
					// 时间大于条件
					$map ['ctime'] = array (
							'GT',
							strtotime ( $_POST ['ctime'] [0] )
					);
				} elseif (! empty ( $_POST ['ctime'] [1] )) {
					// 时间小于条件
					$map ['ctime'] = array (
							'LT',
							strtotime ( $_POST ['ctime'] [1] )
					);
				}
			}
				
		}
		// 查询数据
		$list = $this->where ( $map )->order ( $order )->findPage ( $limit );
		
		
		
		return $list;
	
	}
	
	public function getBannerInfo($id){
		$id = intval ( $id );
		if ($id <= 0) {
			$this->error = L ( 'PUBLIC_UID_INDEX_ILLEAGAL' ); // UID参数值不合法
			return false;
		}
// 		if ($banner = static_cache ( 'banner_info_' . $id )) {
// 			return $banner;
// 		}
// 		// 查询缓存数据
// 		$banner = model ( 'Cache' )->get ( 'bi_' . $id );
		
// 		if (! $banner) {
// 			$this->error = L ( 'PUBLIC_GET_USERINFO_FAIL' ); // 获取用户信息缓存失败
// 			$map ['id'] = $id;
// 			$banner = $this->_getBannerInfo ( $map );
// 		}
// 		static_cache ( 'banner_info_' . $id, $banner );
		$banner = $this->where("id=$id")->find();
		return $banner;
	}
	
	public function _getBannerInfo($map , $field="*"){
		$user = $this->getUserDataByCache($map, $field);
		
		if (! $user) {
			$this->error = L ( 'PUBLIC_GET_INFORMATION_FAIL' ); // 获取用户信息失败
			return false;
		} else {
			$uid = $user ['uid'];
			$user = array_merge ( $user, model ( 'Avatar' )->init ( $user ['uid'] )->getUserAvatar () );
			$user ['avatar_url'] = U ( 'public/Attach/avatar', array (
					'uid' => $user ["uid"]
			) );
			$user ['space_url'] = ! empty ( $user ['domain'] ) ? U ( 'public/Profile/index', array (
					'uid' => $user ["domain"]
			) ) : U ( 'public/Profile/index', array (
					'uid' => $user ["uid"]
			) );
			$user ['space_link'] = "<a href='" . $user ['space_url'] . "' target='_blank' uid='{$user['uid']}' event-node='face_card'>" . $user ['uname'] . "</a>";
			$user ['space_link_no'] = "<a href='" . $user ['space_url'] . "' title='" . $user ['uname'] . "' target='_blank'>" . $user ['uname'] . "</a>";
			// 用户勋章
			$user ['medals'] = model ( 'Medal' )->getMedalByUid ( $user ['uid'] );
			// 用户认证图标
			$groupIcon = array ();
			$userGroup = model ( 'UserGroupLink' )->getUserGroupData ( $uid );
			$user ['user_group'] = $userGroup [$uid];
			foreach ( $userGroup [$uid] as $value ) {
				$groupIcon [] = '<img title="' . $value ['user_group_name'] . '" src="' . $value ['user_group_icon_url'] . '" style="width:auto;height:auto;display:inline;cursor:pointer;" />';
			}
			$user ['group_icon'] = implode ( '&nbsp;', $groupIcon );
				
			model ( 'Cache' )->set ( 'bi_' . $uid, $user, 600 );
			static_cache ( 'user_info_' . $uid, $user );
			return $user;
		}
	}
}
